document.write(`<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>LeetCode Ultimate</title>
</head>
<body>
<div id="leet-code-ultimate-box" class="info">
    <div id="leet-code-ultimate-status"><div></div></div>
    <div id="leet-code-ultimate-message"></div>
    <div id="leet-code-ultimate-loading"><svg viewBox="0 0 1024 1024" xmlns="http://www.w3.org/2000/svg"><path d="M469.333333 128a42.666667 42.666667 0 0 1 42.666667-42.666667c235.648 0 426.666667 191.018667 426.666667 426.666667a42.666667 42.666667 0 1 1-85.333334 0 341.333333 341.333333 0 0 0-341.333333-341.333333 42.666667 42.666667 0 0 1-42.666667-42.666667z"></path></svg></div>
</div>
</body>
</html>`);
window.stop();

const box = document.querySelector("#leet-code-ultimate-box");
const status = document.querySelector("#leet-code-ultimate-status");
const message = document.querySelector("#leet-code-ultimate-message");
const loading = document.querySelector("#leet-code-ultimate-loading");

const state = {
    message: "",
    type: "",
    loading: true
};

const TYPES = {
    INFO: "info",
    SUCCESS: "success",
    WARNING: "warning",
    ERROR: "error"
};

const updateState = (newState) => {
    for (const stateKey in newState) {
        if (state[stateKey] !== undefined) state[stateKey] = newState[stateKey];
    }
    message.innerHTML = state.message;
    box.className = state.type;
    loading.className = state.loading ? "" : "hide";
};

updateState({
    message: "检查登录状态",
    type: TYPES.INFO,
    loading: true
});

getUserStatus().then(res => res.data.userStatus).then(status => {
    if (status.isSignedIn) {
        connection();
    } else {
        updateState({
            message: "请先登录",
            type: TYPES.WARNING,
            loading: false
        });
        alert("将跳转到登录页面");
        window.open(
            "https://leetcode.cn/accounts/login/?next=/playground/?leet-code-ultimate",
            "_self"
        );
    }
});

const connection = () => {
    updateState({
        message: "连接中",
        type: TYPES.INFO,
        loading: true
    });
    const ws = new WebSocket("ws://localhost:41314");
    let error = false;
    ws.onopen = (ev) => {
        updateState({
            message: "已连接到插件",
            type: TYPES.SUCCESS,
            loading: false
        });
    };

    ws.onmessage = (ev) => {
        processTheMessage(JSON.parse(ev.data), ws).then();
    };

    ws.onerror = (ev) => {
        console.log(ev);
        error = true;
        updateState({
            message: "连接插件失败\n2秒后自动重连",
            type: TYPES.ERROR,
            loading: false
        });
    };

    ws.onclose = (ev) => {
        if (error) {
            setTimeout(() => {
                connection();
            }, 2000);
        } else {
            updateState({
                message: "页面将自动关闭",
                type: TYPES.WARNING,
                loading: false
            });
            setTimeout(() => {
                window.close();
            }, 2000);
        }
    };
};